package com.tenton.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenton.MyException.DataNotExist;
import com.tenton.MyException.InsufficientQuantity;
import com.tenton.mapper.ExCommodityMapper;
import com.tenton.mapper.StockMapper;
import com.tenton.pojo.ExCommodity;
import com.tenton.pojo.Stock;
import com.tenton.service.ExCommodityService;
import com.tenton.utils.Constants;
import com.tenton.utils.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@Service
public class ExCommodityServiceImpl implements ExCommodityService {
    @Autowired
    ExCommodityMapper exCommodityMapper;
    @Autowired
    StockMapper stockMapper;

    /**
     * 删除退货信息
     *
     * @param id
     * @param session
     * @return
     */
    @Override
    public void deleteByPrimaryKey(Integer id, HttpSession session) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("退货Id为空");
        } else {
            String name = (String) session.getAttribute("name");
            ExCommodity exCommodity = exCommodityMapper.selectByPrimaryKey(id);
            Stock stock = stockMapper.StockWithCom(exCommodity.getComId());
            //仓库中此商品的库存
            int stockNum = stock.getNum();
            //此次退货数量
            int exCommodityNum = exCommodity.getNum();
            //仓库中商品剩余库存
            stockNum = stockNum + exCommodityNum;
            stock.setNum(stockNum);
            exCommodityMapper.deleteByPrimaryKey(id);
            stock.setUpdateName(name);
            Date date = new Date();
            stock.setUpdateTime(date);
            stockMapper.updateByPrimaryKey(stock);
        }
    }

    /**
     * 新增退货信息
     *
     * @param exCommodity
     * @param session
     * @return
     */
    @Override
    public void insert(ExCommodity exCommodity, HttpSession session) throws DataNotExist {
        if (exCommodity == null) {
            throw new DataNotExist("退货信息为空");
        } else {
            String name = (String) session.getAttribute("name");
            Stock stock = stockMapper.StockWithCom(exCommodity.getComId());
            //此次退货数量
            int exCommodityNum = exCommodity.getNum();
            //仓库中商品库存
            int stockNum = stock.getNum();
            if (stockNum >= exCommodityNum) {
                exCommodity.setCreateName(name);
                Date date = new Date();
                exCommodity.setCreateTime(date);
                exCommodityMapper.insert(exCommodity);
                stockNum -= exCommodityNum;
                stock.setNum(stockNum);
                stock.setUpdateName(name);
                stock.setUpdateTime(date);
                if (stockNum <= 0) {
                    stockMapper.deleteByPrimaryKey(stock.getId());
                } else {
                    stockMapper.updateByPrimaryKey(stock);
                }
            } else {
                throw new NumberFormatException("库存数量不足");
            }
        }
    }

    /**
     * 获取所有退货信息
     *
     * @param pn
     * @param model
     */
    @Override
    public void getAll(Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<ExCommodity> exCommodities = exCommodityMapper.selectAll();
        PageInfo<ExCommodity> pageInfo = new PageInfo<ExCommodity>(exCommodities);
        model.addAttribute("pageInfo", pageInfo);
    }

    /**
     * 查询退货信息
     *
     * @param id
     * @return
     */
    @Override
    public void selectByPrimaryKey(Integer id, Model model) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("退货Id为空");
        } else {
            model.addAttribute("exCommodity", exCommodityMapper.selectByPrimaryKey(id));
        }
    }

    /**
     * 查询退货信息
     *
     * @param id
     * @return
     */
    @Override
    public ExCommodity selectById(Integer id) {
        if (IntegerUtils.isEmpty(id)) {
            throw new NullPointerException("退货Id为空");
        } else {
            return exCommodityMapper.selectByPrimaryKey(id);
        }
    }

    /**
     * 修改退货信息
     *
     * @param exCommodity
     * @param model
     * @param session
     * @return
     */
    @Override
    public Boolean update(ExCommodity exCommodity, Model model, HttpSession session) throws InsufficientQuantity {
        if (exCommodity == null) {
            throw new NullPointerException("退货信息为空");
        } else {
            String name = (String) session.getAttribute("name");
            exCommodity.setUpdateName(name);
            Date date = new Date();
            exCommodity.setUpdateTime(date);
            //未修改时的退货信息
            ExCommodity oldExCommodity = exCommodityMapper.selectByPrimaryKey(exCommodity.getId());
            boolean flag = exCommodityMapper.updateByPrimaryKey(exCommodity);
            if (flag) {
                //修改后的退货信息
                ExCommodity newExCommodity = exCommodityMapper.selectByPrimaryKey(exCommodity.getId());
                Stock stock = stockMapper.StockWithCom(newExCommodity.getComId());
                //原始退货信息
                int oldExCommodityNum = oldExCommodity.getNum();
                //此次退货数量
                int exCommodityNum = newExCommodity.getNum();
                //仓库中商品库存
                int stockNum = stock.getNum();
                //将原始退货数量增加回仓库
                stockNum = stockNum + oldExCommodityNum;
                //判断库存数量是否大于退货数量
                if (stockNum >= exCommodityNum) {
                    stockNum = stockNum - exCommodityNum;
                    stock.setNum(stockNum);
                    stock.setUpdateName(name);
                    stock.setUpdateTime(date);
                    stockMapper.updateByPrimaryKey(stock);
                    model.addAttribute("exCommodity", newExCommodity);
                } else {
                    throw new InsufficientQuantity("商品库存不足");
                }
            }
            return flag;
        }
    }

    /**
     * 商品名称模糊查询
     *
     * @param comName
     * @param pn
     * @param model
     * @return
     */
    @Override
    public void getByParams(String comName, Integer pn, ModelMap model) {
        PageHelper.startPage(pn, Constants.PAGE_SIZE);
        List<ExCommodity> exCommodities = exCommodityMapper.getByParams(comName);
        PageInfo<ExCommodity> pageInfo = new PageInfo<ExCommodity>(exCommodities);
        model.addAttribute("pageInfo", pageInfo);
    }
}
